﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using ImageRecognition2.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.Forms;

using ScriptCompiler2;
namespace ImageRecognition2.Action
{
    /// <summary>
    /// 
    /// </summary>
    public class ActionImageTransformation : AbstractCommand
    {
        protected CReflectiveEvaluator m_ReflectiveEvaluator = new CReflectiveEvaluator();

        /// <summary>
        /// 
        /// </summary>
        public ActionImageTransformation()
        {
            string sLowLevelGraphicsPath = Application.StartupPath + @"\LowLevelGraphics.dll";
            m_ReflectiveEvaluator.AddUsing("System.Windows.Forms.dll");
            m_ReflectiveEvaluator.AddUsing("System.Drawing.dll");
            m_ReflectiveEvaluator.AddUsing(sLowLevelGraphicsPath);

            m_ReflectiveEvaluator.CompilerSuccess += new EventHandler(m_ReflectiveEvaluator_CompilerSuccess);
            m_ReflectiveEvaluator.CompilerError += new CompilerErrorEventHandler(m_ReflectiveEvaluator_CompilerError);
            m_ReflectiveEvaluator.TypeCreationError += new TypeCreationErrorEventHandler(m_ReflectiveEvaluator_TypeCreationError);
        }

        /// <summary>
        /// Type creation error
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ReflectiveEvaluator_TypeCreationError(object sender, TypeCreationErrorEventArgs e)
        {
            MessageBox.Show(string.Format("Der Typ '{0}' konnte nicht erstellt werden.", e.TypeName));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ReflectiveEvaluator_CompilerError(object sender, CompilerErrorEventArgs e)
        {
            //MessageBox.Show(e.Source + Environment.NewLine + " " + e.CompilerErrorCollection[0].ErrorText);
            Debug.WriteLine(e.Source + Environment.NewLine + e.CompilerErrorCollection[0].ErrorText);
        }

        /// <summary>
        /// Handles the CompilerSuccess event of the m_ReflectiveEvaluator control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void m_ReflectiveEvaluator_CompilerSuccess(object sender, EventArgs e)
        {
            Debug.WriteLine("Successfull!");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Run(object sender, EventArgs e)
        {
            FormGeoTransformFormulaScript form = new FormGeoTransformFormulaScript();
            if (form.ShowDialog() != DialogResult.OK) return;

            string sTextX = form.tbXTransform.Text;
            string sTextY = form.tbYTransform.Text;
            UnsafeBitmap bitmap = ExecuteCompiledScript(sTextX, sTextY);
            bitmap.Dispose();
            MainFormSingleton.ActivePictureBox.BackgroundImage = bitmap.InternalBitmap;
            MainFormSingleton.ActivePictureBox.Invalidate();
        }

        /// <summary>
        /// Executes the compiled script.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <returns></returns>
        private UnsafeBitmap ExecuteCompiledScript(string _sTextX, string _sTextY)
        {
            UnsafeBitmap bitmap = MainFormSingleton.ActivePictureBox.BackgroundImage;
            string sScript = Definitions.FormulaScript;
            sScript = sScript.Replace("//{0}", _sTextX);
            sScript = sScript.Replace("//{1}", _sTextY);
            UnsafeBitmap newUnsafeBitmap = (UnsafeBitmap)bitmap.Clone();
            m_ReflectiveEvaluator.Eval(sScript, "FormulaScript", "Execute", newUnsafeBitmap);
            return newUnsafeBitmap;
        }


        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return base.Clone();
        }
    }
}
